/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TOpLog;
import com.ruipengkj.commons.base.query.BaseQuery;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.dao.TOpLogDao;
import com.ruipengkj.service.TOpLogService;
/**
 * 说明：
 * 操作日志Service层接口实现
 * @Author：李智深
 * @Date：2012-03-16
 */
@Service
public class TOpLogServiceImpl implements TOpLogService{
	@Resource
	private TOpLogDao TOpLogDao;//DAO层
	//查询所有
	@Override
	public List<TOpLog> findService(TOpLog entity) throws Exception {
		String hql = "select o from TOpLog o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TOpLogDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
	//根据ID翻页查询
	@Override
	public PageModel<TOpLog> getScrollService(int firstResult,
			int maxResult, TOpLog entity) throws Exception {
		String hql = "select o from TOpLog o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getTEmployee()!=null){
				if(entity.getTEmployee().getName()!=null&&!entity.getTEmployee().getName().equals("")){
					whereHQL.append("TEmployee.name").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
					param.add("%"+entity.getTEmployee().getName()+"%");
				}
				if(entity.getTEmployee().getEmployeeSn()!=null&&!entity.getTEmployee().getEmployeeSn().equals("")){
					whereHQL.append("TEmployee.employeeSn").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
					param.add("%"+entity.getTEmployee().getEmployeeSn()+"%");
				}
				if(entity.getTEmployee().getCardNum()!=null&&!entity.getTEmployee().getCardNum().equals("")){
					whereHQL.append("TEmployee.cardnum").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
					param.add("%"+entity.getTEmployee().getCardNum()+"%");
				}
				if(entity.getOperatestr()!=null&&!entity.getOperatestr().equals("")){
					whereHQL.append("Operate").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
					param.add("%"+entity.getOperatestr()+"%");
				}
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("happenTime", BaseQuery.DESC);
		return TOpLogDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}
	//修改
	@Override
	public void modifyService(TOpLog entity) throws Exception {
		TOpLogDao.modifyDao(entity);
	}
	//添加
	@Override
	public void saveService(TOpLog entity) throws Exception {
		TOpLogDao.saveDao(entity);
	}
	//根据ID删除
	@Override
	public void deleteService(String id) throws Exception {
		TOpLogDao.deleteDao(id);
	}
	//根据ID查询
	@Override
	public TOpLog getByIdService(String id) throws Exception {
		return TOpLogDao.getByIdDao(id);
	}
	//添加或修改
	@Override
	public void saveOrUpdateService(TOpLog entity) throws Exception {
		TOpLogDao.saveOrUpdateDao(entity);
	}
	//添加返回ID
	@Override
	public Serializable saveRetService(TOpLog entity) throws Exception {
		return TOpLogDao.saveRetDao(entity);
	}
	public TOpLogDao getTOpLogDao() {
		return TOpLogDao;
	}

	public void setTOpLogDao(TOpLogDao TOpLogDao) {
		this.TOpLogDao = TOpLogDao;
	}
	@Override
	public void addOpLogService(String log) throws Exception {
		TOpLog opLog=new TOpLog();
		opLog.setLogID(UUID.GenUUID15());//日志编号CHAR(15) PK
		opLog.setOperate(log);//操作VARCHAR(256)
		opLog.setHappenTime(new Date());//操作日期DATETIME
		opLog.setTEmployee(ApplicationContext.EMPLOYEE);//员工
		TOpLogDao.saveDao(opLog);
	}
}